/*
 * FlightController_Calibration.h
 *
 *  Created on: Jan 20, 2014
 *      Author: ppks
 */

#ifndef FLIGHTCONTROLLER_CALIBRATION_H_
#define FLIGHTCONTROLLER_CALIBRATION_H_

#include "FlightController.h"
#include "types.h"
#include "defines.h"

/********************************************************************************/
/* Constants Definition                                                         */
/********************************************************************************/
#if ACC_INSTALLED
    #define ACC_CALIBRATED                  0x01
#else /* ACC_INSTALLED */
    #define ACC_CALIBRATED                  0x00
#endif /* ACC_INSTALLED */

#if GYRO_INSTALLED
    #define GYRO_CALIBRATED                 0x02
#else /* GYRO_INSTALLED */
    #define GYRO_CALIBRATED                 0x00
#endif /* GYRO_INSTALLED */

#if MAG_INSTALLED
    #define MAG_CALIBRATED                  0x04
#else /* MAG_INSTALLED */
    #define MAG_CALIBRATED                  0x08
#endif /* MAG_INSTALLED */

#if BARO_INSTALLED
    #define BARO_CALIBRATED                 0x08
#else /* BARO_INSTALLED */
    #define BARO_CALIBRATED                 0x00
#endif /* BARO_INSTALLED */

#define ALL_SENSORS_CALIBRATED              (ACC_CALIBRATED | GYRO_CALIBRATED | MAG_CALIBRATED | BARO_CALIBRATED)

#define UPDATE_CAL_DATA_AFTER_ANY_SENSOR    0x01    /* If this flag is set the firmware will update data in
                                                       EEPROM after any sensor finishes calibration instead of
                                                       waiting for all sensors to finish */

/********************************************************************************/
/* Default Values                                                               */
/********************************************************************************/
/* Default Claibration Data */
/* Note: Mag_Calibrated is set by default since calibration
 * has to be requested by GUI to perform proper calibration */
#define CALIBRATION_GLOBAL_DEFAULTS \
{ \
    MAG_CALIBRATED, /* uint8_t calibrationFlag  */ \
    {0, 0, 0},      /* int16_t accBaseline      */ \
    {0, 0, 0},      /* int16_t gyroBaseline     */ \
    {0, 0, 0},      /* int16_t magBaseline      */ \
    0,              /* uint16_t checksum        */ \
}

#define ACC_CALIBRATION_CYCLES_DEFAULT      512
#define GYRO_CALIBRATION_CYCLES_DEFAULT     512
#define MAG_CALIBRATION_CYCLES_DEFAULT      200
#define BARO_CALIBRATION_CYCLES_DEFAULT     200


/********************************************************************************/
/* Macro Definition                                                             */
/********************************************************************************/
#define IsAccCalibrated()           (calData.calibrationFlag & ACC_CALIBRATED)
#define IsGyroCalibrated()          (calData.calibrationFlag & GYRO_CALIBRATED)
#define IsMagCalibrated()           (calData.calibrationFlag & MAG_CALIBRATED)
#define IsBaroCalibrated()          (calData.calibrationFlag & BARO_CALIBRATED)

#define SetAccCalibratedFlag()      (calData.calibrationFlag |= ACC_CALIBRATED)
#define ClearAccCalibratedFlag()    (calData.calibrationFlag &= ~ACC_CALIBRATED)
#define SetGyroCalibratedFlag()     (calData.calibrationFlag |= GYRO_CALIBRATED)
#define ClearGyroCalibratedFlag()   (calData.calibrationFlag &= ~GYRO_CALIBRATED)
#define SetMagCalibratedFlag()      (calData.calibrationFlag |= MAG_CALIBRATED)
#define ClearMagCalibratedFlag()    (calData.calibrationFlag &= ~MAG_CALIBRATED)
#define SetBaroCalibratedFlag()     (calData.calibrationFlag |= BARO_CALIBRATED)
#define ClearBaroCalibratedFlag()   (calData.calibrationFlag &= ~BARO_CALIBRATED)

/********************************************************************************/
/* Exported Variables Declaration                                               */
/********************************************************************************/

/********************************************************************************/
/* Exported Functions Prototypes                                                */
/********************************************************************************/
void FC_LoadCalibrationDefaults(void);
bool FC_IsDeviceCalibrationDone(void);

#endif /* FLIGHTCONTROLLER_CALIBRATION_H_ */
